<template>
  <el-dialog :visible.sync="visible" :close-on-click-modal="false" width="60%" :show-close="false" class="contract-from" @close="closeVisble">
    <span slot="title" class="dialog-title">
      <div class="dialog-title-left">
        {{ title }}
      </div>
      <div class="dialog-title-right">
        <el-button @click="closeVisble">取 消</el-button>
        <el-button size="small" type="primary" @click="submitForm('ruleForm')">提 交</el-button>
      </div>
    </span>
    <el-form ref="ruleForm" label-position="top" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="部门名称" prop="department_name">
            <el-input v-model="ruleForm.department_name" placeholder="请输入部门名称" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="部门负责人" prop="department_leader_id">
            <!-- <el-input v-model="ruleForm.department_leader_id" placeholder="请输入部门负责人" /> -->
            <el-select
              v-model="ruleForm.department_leader_id"
              filterable
              remote
              reserve-keyword
              placeholder="部门负责人"
              :remote-method="remoteMethod"
              :loading="loading"
              style="width: 100%"
            >
              <el-option
                v-for="item in options"
                :key="item.ID"
                :label="item.Name"
                :value="item.ID"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import {
  getEmpSearch } from '@/api/employee'
import { addDepartment, putDepartment } from '@/api/base'
import { rulesDep } from './config'
export default {
  name: 'LevelFrom',
  props: {
    title: {
      type: String,
      default: '部门管理'
    },
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: Number,
      default: 0
    },
    form: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      ruleForm: {
        department_name: '',
        department_leader_id: null
      },
      loading: false,
      options: [],
      rules: rulesDep
    }
  },
  watch: {
    visible: {
      handler(value) {
        if (value && this.id) {
          this.ruleForm = this.form
          const op = []
          if (this.form.leader) {
            const financeObj = {
              ID: this.form.leader.ID,
              Name: this.form.leader.name
            }
            op.push(financeObj)
          }
          this.options = op
        }
      }
    }
  },
  methods: {
    async remoteMethod(query) {
      if (query !== '') {
        this.loading = true
        const res = await getEmpSearch(query)
        this.options = res.data
        setTimeout(() => {
          this.loading = false
        }, 200)
      } else {
        this.options = []
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.id) {
            this.editContracts()
          } else {
            this.addContracts()
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    async addContracts() {
      const prams = { ...this.ruleForm }
      const res = await addDepartment(prams)
      if (res.ret === 0) {
        this.$message.success('新增成功')
        this.$emit('success')
      }
    },
    async editContracts() {
      const prams = { ...this.ruleForm, id: this.id }
      const res = await putDepartment(prams)
      if (res.ret === 0) {
        this.$message.success('更新成功')
        this.$emit('success')
      }
    },
    closeVisble() {
      if (this.$refs['ruleForm']) {
        this.$refs['ruleForm'].resetFields()
      }
      this.ruleForm = {
        department_name: '',
        department_leader_id: null
      }
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss">
.contract-from {
  .el-dialog__header {
    padding: 0;
  }
  .el-input-number {
    text-align: left!important;
  }
}
</style>
